﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.Mvc;

using mvcForum.Core;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Specifications;
using mvcForum.Web.Controllers;
using mvcForum.Web.ViewModels;
using mvcForum.Web.Areas.ForumAdmin.Helpers;
using mvcForum.Web.Interfaces;
using mvcForum.Core.Interfaces.Services;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.Web.Areas.ForumAdmin.Controllers {

	public class BoardController : ForumAdminBaseController {
		private readonly IRepository<Board> boardRepo;
		private readonly IMembershipService membershipService;

		public BoardController(IWebUserProvider userProvider, IContext context, IMembershipService membershipService)
			: base(userProvider, context) {

			this.boardRepo = this.GetRepository<Board>();
			this.membershipService = membershipService;
		}

		#region Create
		public ActionResult Create() {
			if (Request.IsAjaxRequest()) {
				return PartialView("CreateBoardPartial", new BoardViewModel());
			}
			return View(new BoardViewModel());
		}

		[HttpPost]
		public ActionResult Create(BoardViewModel model) {
			if (this.membershipService.IsAccountInRole(this.ActiveUser.EmailAddress, Constants.Roles.SolutionAdmin)) {
				if (ModelState.IsValid) {
					// TODO:
					Board board = new Board { Name = model.Name };
					this.boardRepo.Create(board);
					this.Context.SaveChanges();

					if (Request.IsAjaxRequest()) {
						return Json(true);
					}

					return RedirectToAction("Index", "Home");
				}
			}
			else {
				TempData.Add("Reason", "You do not have access to creating new boards");
				return RedirectToAction("NoAccess", "Forum");
			}

			return View();
		}
		#endregion

		#region Update
		public ActionResult Update(Int32 id) {
			// TODO: Validate access!
			Board board = this.GetRepository<Board>().Read(id);

			return View(new mvcForum.Web.Areas.ForumAdmin.ViewModels.Board { Id = id, Path = BreadcrumbHelper.BuildPath(board, Url) });
		}
		#endregion

		#region Delete
		public ActionResult Delete(int id) {
			// TODO: Access!!
			var model = new BoardViewModel(this.boardRepo.Read(id));
			return View(model);
		}

		[HttpPost]
		public ActionResult Delete(int id, bool confirm) {
			// TODO: Access!!
			Board board = this.boardRepo.Read(id);
			if (ModelState.IsValid && confirm) {
				boardRepo.Delete(board);
				this.Context.SaveChanges();

				if (Request.IsAjaxRequest()) {
					return Json(true);
				}

				return RedirectToAction("Index", "Home");
			}

			if (Request.IsAjaxRequest()) {
				return Json(false);
			}

			var model = new BoardViewModel(board);
			return View(model);
		}
		#endregion


		public ActionResult AttachmentSettings() {
			return View();
		}

		public ActionResult Features() {
			return View();
		}

		public ActionResult AvatarSettings() {
			return View();
		}

		public ActionResult PrivateMessagingSettings() {
			return View();
		}

		public ActionResult PostSettings() {
			return View();
		}

		public ActionResult SignatureSettings() {
			return View();
		}
	}
}